module SpreadsheetToJson
  class Config
    class << self
      # Get the spreadsheet_key
      def get_spreadsheet_key(config_path)
        get_settings_from_yml(config_path)['spreadsheet_key']
      end
  
      # Get the access_token of the auth
      def get_access_token(config_path)
        config = get_settings_from_yml(config_path)
        client = OAuth2::Client.new(
          config['auth']['client_id'],
          config['auth']['client_secret'],
          site: config['auth']['site'],
          token_url: config['auth']['token_url'],
          authorize_url: config['auth']['token_url']
        )
        auth_token = OAuth2::AccessToken.from_hash(
          client,
          {
            :refresh_token => config['auth']['refresh_token'],
            :expires_at => 3600
          }
        )
        auth_token.refresh!.token
      end

      private
      # Get config file
      def get_settings_from_yml(path)
        File.open(path) { |file| YAML.load(file) }
      end
    end
  end
end
